﻿using Hr.IDAL;
using Hr.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Hr.DAL
{
    public partial class MenuDAL:BaseDAL<Sys_Menu>,ISys_MenuDAL
    {
        private DbContext db = DbContextFactory.Create();
        #region 菜单管理DAL
        #region 获取数据
        /// <summary>
        /// 获取所有菜单信息集合,包括模糊查询
        /// </summary>
        /// <returns></returns>
        public IEnumerable<object> GetMenuList(string Term)
        {
            List<Sys_TypeID> TypeList = GetTypeList("Sys_Menu");
           return (from page in GetMenuPage(0)
                    where page.Name_CHS.Contains(Term)
                    select new
                    {
                        page.pk_Menu,
                        page.ParentID,
                        page.Name_CHS,
                        page.Url,
                        page.Icon,
                        page.SortID,
                        page.TypeID,
                        page.Visible,
                        TypeName_CHS = TypeList.Where(n=>n.ID== page.TypeID).FirstOrDefault().TypeName_CHS,
                        Visual = TypeList.Where(n => n.ID == page.TypeID).FirstOrDefault().Visual
                    });
        }

        
        /// <summary>
        /// 通过表名获取类型集合
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<Sys_TypeID> GetTypeList(string name)
        {
            return db.Set<Sys_TypeID>().Where(n => n.TableName.Equals(name)).ToList();
        }

        //通过ID获取绑定的所有菜单
        public IEnumerable<object> GetUserMenuList(int pk_User)
        {
            //根据用户ID查询出用户信息来
            User_List U = db.Set<User_List>().SingleOrDefault(n => n.pk_User == pk_User);
            //循环的是用户绑定且能访问的页面集合
            List<object> RoleList = new List<object>();
            List<Sys_Menu> SML = (from SM in db.Set<Sys_Menu>()
                                       join RL in db.Set<Role_List>()
                                       on SM.pk_Menu equals RL.fk_Menu
                                       where RL.fk_RoleName == U.RoleValue
                                       & RL.IsUse == true
                                       //&SM.Visible==true
                                  select SM).ToList();

            return GetUserMenu(0,SML);
        }

        public IEnumerable<object> GetUserMenu(int parent,List<Sys_Menu> SML)
        {
            List<object> MList = new List<object>();
            //循环所有的

            
            // return SML;
            foreach (Sys_Menu item in SML.Where(n=>n.ParentID==parent).OrderBy(o=>o.SortID))
            {
                //先从父级菜单开始查找
                //判断是否有子项
                if (db.Set<Sys_Menu>().Where(n => n.ParentID == item.pk_Menu).Count() > 0)
                {
                    //有子集则继续
                    //递归
                    Dictionary<string, object> dt = new Dictionary<string, object>()
                    {
                        //{"path",item.Name_CHS },
                        //{"ParentID",item.ParentID },
                        {"path",item.Url },
                        {"component",item.Tag_CHS },
                        {"IsShow",item.Visible},
                        {"meta",new{
                        title=item.Name_CHS,
                        IsSlid=item.TypeID==0?true:false,
                        IconCls=item.Icon
                        } },
                        {"children",GetUserMenu(item.pk_Menu, SML) }
                    };
                    MList.Add(dt);
                }
                else
                {
                    Dictionary<string, object> dt = new Dictionary<string, object>()
                    {
                        //{"path",item.Name_CHS },
                        //{"ParentID",item.ParentID },
                        {"path",item.Url },
                        {"component",item.Tag_CHS },
                        {"meta",new{
                        title=item.Name_CHS,
                        IsSlid=item.TypeID==0?true:false,
                        IconCls=item.Icon
                        } },
                        {"children",null }
                    };
                    MList.Add(dt);
                }
            }
            return MList;
        }
        #region 递归
        //获取页面集合并排序,本类访问
        private List<Sys_Menu> GetMenuPage(int parentID)
        {
            List<Sys_Menu> lst = new List<Sys_Menu>();
            foreach (Sys_Menu item in db.Set<Sys_Menu>().Where(n => n.ParentID == parentID).OrderBy(n => n.SortID))
            {
                lst.Add(item);
                List<Sys_Menu> ChildrenSys = new List<Sys_Menu>();
                if (db.Set<Sys_Menu>().Where(n => n.ParentID == item.pk_Menu).Count() > 0)
                {
                    //递归
                    ChildrenSys = GetMenuPage(item.pk_Menu).OrderBy(n => n.SortID).ToList();
                    lst.AddRange(ChildrenSys);
                }
            }
            return lst;
        }
        #endregion

        #endregion

        #region 添加、修改、删除
        /// <summary>
        /// 添加、修改菜单
        /// </summary>
        /// <param name="M"></param>
        /// <param name="Type"></param>
        public void UpdateMenu(Sys_Menu M,string Type)
        {
            if (Type.Equals("Add"))
            {
                //添加菜单
                db.Set<Sys_Menu>().Add(M);
            }else if (Type.Equals("Edit"))
            {
                //修改菜单
                db.Set<Sys_Menu>().AddOrUpdate(M);
            }
        }

        public void DelMenu(int pk_Menu)
        {
            //查出要删除的所有菜单
            List<Sys_Menu> MenuList = db.Set<Sys_Menu>().Where(n => n.pk_Menu == pk_Menu || n.ParentID == pk_Menu).ToList();
            //删除菜单集合
            db.Set<Sys_Menu>().RemoveRange(MenuList);
        }

        #endregion

        #endregion

        #region 类型管理DAL
        /// <summary>
        /// 获取所有类型集合信息,包括模糊查询
        /// </summary>
        /// <returns></returns>
        public List<Sys_TypeID> GetTypeListAll(string Term)
        {
            return db.Set<Sys_TypeID>().Where(n => n.TypeName_CHS.Contains(Term)).ToList();
        }



        /// <summary>
        /// 获取审核人信息
        /// </summary>
        /// <returns></returns>
        public IEnumerable<object> GetAuditList(string Term)
        {
            //获取审核人信息，角色表、部门表、用户表
            if (Term == null)
            {
                Term = "";
            }
            return (from UL in db.Set<User_List>()
                    join UD in db.Set<User_Department>()
                    on UL.fk_Department equals UD.pk_Department
                    join RN in db.Set<Role_Name>()
                    on UL.RoleValue equals RN.pk_Name
                    where UL.FullName.Contains(Term)||UD.DepartmentName.Contains(Term)||UL.UserName.Contains(Term)
                    select new
                    {
                        ImagePath1=UL.ImagePath1.Length>0?UL.ImagePath1:"/",
                        UL.UserName,
                        UL.FullName,
                        UL.Phone1,
                        RN.RoleName_CHS,
                        UD.DepartmentName,
                        UL.pk_User
                    });
        }

        /// <summary>
        /// 根据类型ID,获取类型信息
        /// </summary>
        /// <param name="pk_TypeID"></param>
        /// <returns></returns>
        public Sys_TypeID GetTypeInfo(int pk_TypeID)
        {
            return db.Set<Sys_TypeID>().SingleOrDefault(n => n.pk_TypeID == pk_TypeID);
        }

        /// <summary>
        /// 修改、添加类型
        /// </summary>
        /// <param name="T"></param>
        /// <param name="pk_User"></param>
        public void UpdateType(Sys_TypeID T,string Type,int pk_User)
        {
            if (Type.Equals("Add"))
            {
                //添加
                db.Set<Sys_TypeID>().Add(T);
            }else if (Type.Equals("Edit"))
            {
                //修改
                db.Set<Sys_TypeID>().AddOrUpdate(T);
            }
        }

        /// <summary>
        /// 删除类型
        /// </summary>
        /// <param name="T"></param>
        public void DelType(Sys_TypeID T)
        {
            db.Set<Sys_TypeID>().Remove(T);
        }
        #endregion
        public bool SaveChanges()
        {
            return db.SaveChanges() > 0;
        }
    }
}
